package cn.herlioner.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import cn.herlioner.dao.JudgeDAO;
import cn.herlioner.pojo.Judge;
import cn.herlioner.pojo.TSObject;

public class JudgeDAOImpl implements JudgeDAO{

	@Override
	public void add(Judge j) {
		SessionFactory sf = new Configuration().configure()
				.buildSessionFactory();
		Session s = sf.openSession();
		s.beginTransaction();
		
		s.save(j);
		
		s.getTransaction().commit();
		s.close();
		sf.close();
		
	}

	@Override
	public Judge get(int judge) {
		Judge result = null;
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        result = (Judge) s.get(Judge.class, judge);
 
        s.close();
        sf.close();
        return result;
	}

	@Override
	public void delete(int judge) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        Judge p = (Judge) s.get(Judge.class, judge);
	 
	        s.delete(p);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public void update(Judge j) {
		 SessionFactory sf = new Configuration().configure()
	                .buildSessionFactory();
	        Session s = sf.openSession();
	        s.beginTransaction();
	 
	        s.update(j);
	 
	        s.getTransaction().commit();
	        s.close();
	        sf.close();
		
	}

	@Override
	public List<Judge> list() {
		List<Judge> result = new ArrayList();
		 
        SessionFactory sf = new Configuration().configure()
                .buildSessionFactory();
        Session s = sf.openSession();
 
        Query q = s.createQuery("from Judge j");
 
        result = q.list();
 
        s.close();
        sf.close();
        return result;
	}

}
